package com.dengyafei.service.impl;

import com.dengyafei.bean.DeliveryEntity;
import com.dengyafei.bean.DeliveryEntityExample;
import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.bean.GraduateEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.domin.dto.*;
import com.dengyafei.domin.vo.*;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.DeliveryEntityMapper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.mapper.GraduateEntityMapper;
import com.dengyafei.mapper.PreferEntityMapper;
import com.dengyafei.result.PageEntity;
import com.dengyafei.service.DeliveryService;
import com.dengyafei.utils.JwtUtil;
import com.dengyafei.utils.RecommendUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 邓亚非
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {

    @Autowired
    DeliveryEntityMapper deliveryEntityMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    DeliveryEntityExample deliveryEntityExample;
    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;
    @Autowired
    GraduateEntityMapper graduateEntityMapper;
    @Autowired
    PreferEntityMapper preferEntityMapper;

    /**
     * 查询投递岗位
     *
     * @param pageVo
     * @return
     */
    @Override
    public PageEntity<DeliveryEntity> queryByHid(PageVo pageVo) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        PageEntity<DeliveryEntity> pageEntity = new PageEntity<>();
        DeliveryEntityExample.Criteria criteria = deliveryEntityExample.createCriteria().andHidEqualTo(enterpriseEntity.getHid());
        Page<Object> page = PageHelper.startPage(pageVo.getCurrent(), pageVo.getPageSize());
        List<DeliveryEntity> deliveryEntities = deliveryEntityMapper.selectByExample(deliveryEntityExample);
        pageEntity.setCurrent(pageVo.getCurrent());
        pageEntity.setPageSize(pageVo.getPageSize());
        pageEntity.setTotalCounts(page.getTotal());
        pageEntity.setPages(page.getPages());
        pageEntity.setList(deliveryEntities);
        return pageEntity;
    }

    /**
     * 拒绝投递
     *
     * @param deliveryRefuseVO
     * @return
     */
    @Override
    public Integer refuseDelivery(DeliveryRefuseVO deliveryRefuseVO) {
        DeliveryRefuseDTO deliveryRefuseDTO = BeanHelper.copyProperties(deliveryRefuseVO, DeliveryRefuseDTO.class);
        Integer integer = deliveryEntityMapper.updateStatus(deliveryRefuseDTO.getDid(), RecommendConstant.PROCESS_STOP);
        return integer;
    }

    /**
     * 投递简历
     *
     * @param deliveryResumeVO
     * @return
     */
    @Override
    public Integer deliveryResume(DeliveryResumeVO deliveryResumeVO) {
//        先插入投递表
        DeliveryResumeDTO deliveryResumeDTO = BeanHelper.copyProperties(deliveryResumeVO, DeliveryResumeDTO.class);
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        GraduateEntity graduateEntity = graduateEntityMapper.queryAllByGname(userInfo);
//        判断是否已经投递
        DeliveryEntity deliveryEntity = deliveryEntityMapper.queryByPidAndGid(deliveryResumeVO.getPid(), graduateEntity.getGid());
        if (!ObjectUtils.isEmpty(deliveryEntity)) {
            return RecommendConstant.ALREADY_DELIVERY;
        }
        deliveryResumeDTO.setGid(graduateEntity.getGid());
//        拿到分数
        Integer preferVal = RecommendUtil.getPreferVal();
        deliveryResumeDTO.setLval(preferVal);
        Integer integer = deliveryEntityMapper.insertDelivery(deliveryResumeDTO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            return integer;
        }
        return preferEntityMapper.insertPrefer(deliveryResumeDTO);
    }

    /**
     * 根据gid查询投递岗位
     *
     * @param pageVo
     * @return
     */
    @Override
    public PageEntity<DeliveryEntity> queryByGid(PageVo pageVo) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        GraduateEntity graduateEntity = graduateEntityMapper.queryAllByGname(userInfo);
        DeliveryEntityExample.Criteria criteria = deliveryEntityExample.createCriteria().andGidEqualTo(graduateEntity.getGid());
        Page<Object> page = PageHelper.startPage(pageVo.getCurrent(), pageVo.getPageSize());
        List<DeliveryEntity> deliveryEntities = deliveryEntityMapper.selectByExample(deliveryEntityExample);
        PageEntity<DeliveryEntity> pageEntity = new PageEntity<>();
        pageEntity.setCurrent(pageVo.getCurrent());
        pageEntity.setPageSize(pageVo.getPageSize());
        pageEntity.setPages(page.getPages());
        pageEntity.setTotalCounts(page.getTotal());
        pageEntity.setList(deliveryEntities);
        return pageEntity;
    }

    /**
     * 撤销投递
     *
     * @param deliveryRevokeVO
     * @return
     */
    @Override
    public Integer revokeDelivery(DeliveryRevokeVO deliveryRevokeVO) {

        return deliveryEntityMapper.updateStatus(deliveryRevokeVO.getDid(), RecommendConstant.REVOKE_DELIVERY);
    }

    /**
     * 删除投递记录
     *
     * @param deliveryDeleteVO
     * @return
     */
    @Override
    public Integer deleteDelivery(DeliveryDeleteVO deliveryDeleteVO) {
        return deliveryEntityMapper.deleteByPrimaryKey(deliveryDeleteVO.getDid());
    }

    /**
     * 热度排名前五的职位
     * @return
     */
    @Override
    public List<DeliveryNumberPositionDTO> queryDeliveryNumberPosition() {
        return deliveryEntityMapper.queryDeliveryNumberPosition();
    }

    /**
     * 热度排名前五的公司
     * @return
     */
    @Override
    public List<DeliveryNumberCompanyDTO> queryDeliveryNumberCompany() {
        List<Integer> list = deliveryEntityMapper.queryNumberHid();
        List<String> companies = enterpriseEntityMapper.queryCompanies(list);
        List<DeliveryNumberHidDTO> deliveryNumberHidDTOS = deliveryEntityMapper.queryNumbers();
        List<DeliveryNumberCompanyDTO> deliveryNumberCompanyDTOS =new ArrayList<>();
        for (String company : companies) {
            DeliveryNumberCompanyDTO deliveryNumberCompanyDTO = new DeliveryNumberCompanyDTO();
            deliveryNumberCompanyDTO.setCompany(company);
            deliveryNumberCompanyDTOS.add(deliveryNumberCompanyDTO);
        }
        for (int i = 0; i < deliveryNumberHidDTOS.size(); i++) {
            deliveryNumberCompanyDTOS.get(i).setNumber(deliveryNumberHidDTOS.get(i).getNumber());
        }
        return deliveryNumberCompanyDTOS;
    }
}